package com.bingkaimedia.dompetobeta.app;
 
  
 

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.bingkaimedia.dompetobeta.R;
import com.bingkaimedia.dompetobeta.libs.AlertDialogManager;
import com.bingkaimedia.dompetobeta.libs.JSONfunctions;
import com.bingkaimedia.dompetobeta.libs.LazyAdapter2;
import com.bingkaimedia.dompetobeta.libs.SessionManager;
import com.bingkaimedia.dompetobeta.libs.parser_Json;
import com.bingkaimedia.dompetobeta.libs_image.ImageLoader;
import com.bingkaimedia.dompetobeta.libs_slidemenu.BaseActivity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable; 
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.ParseException;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message; 
import android.telephony.TelephonyManager;
import android.text.Editable; 
import android.text.TextWatcher;
import android.util.DisplayMetrics; 
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView; 
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
  

@SuppressLint("NewApi")
public class Signup  extends Activity  {
	 
	int current_page = 0;
	 
	// XML node keys 
	//static final String KEY_SONG = "song"; // parent node
	
	public static final String KEY_COUNTRIES_ID = "id_countries";  
	public static final String KEY_COUNTRIES_CODE = "countryCode";  
	public static final String KEY_COUNTRIES_NAME = "countryName";  
	public static final String KEY_COUNTRIES_PHONE = "countryPhone";  
	
	
	public static final String KEY_VOUCHER_ID = "voucher_id";
	public static final String KEY_VOUCHER_NAME = "voucher_name"; 
	public static final String KEY_VOUCHER_TOTAL = "voucher_total";
	public static final String KEY_VOUCHER_TERSEDIA = "voucher_tersedia";
	public static final String KEY_VOUCHER_DETAIL = "voucher_detail";
	public static final String KEY_VOUCHER_DATE = "voucher_date";
	public static final String KEY_VOUCHER_DESCRIPTION = "voucher_description";
	public static final String KEY_MERCHANT_ID = "merchant_id";
	public static final String KEY_MERCHANT_NAME = "merchant_name";
	public static final String KEY_MERCHANT_CATEGORY = "merchant_category";
	public static final String KEY_MERCHANT_PHOTO = "merchant_photo";
	public static final String KEY_TRANSACTION_ID = "transaction_id";
	public static final String KEY_TRANSACTION_DATE = "transaction_date";
	public static final String KEY_TRANSACTION_IMAGE = "transaction_image";
	public static final String KEY_TRANSACTION_STATUS = "transaction_status";
	
	public static final String KEY_MY_CHANNEL_ID = "my_channel_id";
	public static final String KEY_CHANNEL_ID = "channel_id";
	public static final String KEY_USERS_ID = "users_id";
	public static final String KEY_CHANNEL_NAME = "channel_name";
	public static final String KEY_CHANNEL_ADDRESS = "channel_address";
	public static final String KEY_CHANNEL_PHOTO = "channel_photo";
	public static final String KEY_CHANNEL_NOTE = "channel_note";
	public static final String KEY_CHANNEL_NOTUSED = "channel_note";
	
	 
	
	
	private String xml_countries_id;
	private String xml_countries_code;
	private String xml_countries_name;
	private String xml_countries_phone;
	 
	
	private String session_userid;
	private String session_username;
	private String session_password;
	private String session_email;
	private String session_city;
	private String session_checked;
	private String session_channel;
	private String session_category;
    

	private int count;
	private String json = "null"; 
	private JSONObject jsonobject;
	public String code;
	public String msg;
    
    public ImageLoader imageLoader; 
    
    private String idtransaksi;
    

	private AutoCompleteTextView autoComplete;
	private AutoCompleteTextView autoComplete2;
	 
 // Session Manager Class
 	SessionManager session;
 	
 	LazyAdapter2 adapter;
 	ArrayList<HashMap<String, String>> menuItems;
 	ProgressDialog pDialog;
 	HashMap<String, String> map = new HashMap<String, String>();
 	
 	ArrayList<HashMap<String, String>> diskonba_list = new ArrayList<HashMap<String, String>>();
 	

	public int i = 0;
 	//slide menu
 	private LinearLayout slidingPanel;
 	private boolean isExpanded;
 	private DisplayMetrics metrics;
 	private RelativeLayout headerPanel;
 	private int panelWidth;
 	private int panelWidth1;

 	private String sta="";
 	private String id_;
 	ArrayList nameValuePairs;
	String result = null;
	InputStream is = null;
	StringBuilder sb=null;
	ImageLoader imgLoader;
	String tanggal1,tanggal2;
 	private ImageView menuViewButton,menuRightButton;

 	FrameLayout.LayoutParams menuPanelParameters;
 	FrameLayout.LayoutParams slidingPanelParameters;
 	LinearLayout.LayoutParams headerPanelParameters;
 	LinearLayout.LayoutParams listViewParameters;
 	
 	private TextView lbl_signup_judul;
 	private TextView lbl_signup_country; 
 	private EditText txt_signup_nama;
 	private EditText txt_signup_email;
 	private EditText txt_signup_password;
 	private EditText txt_signup_password2;
 	private EditText txt_signup_country;
 	private EditText txt_signup_phone;
 	private EditText txt_signup_city;
	private Button cmd_signup_submit;
	private Spinner spinnerFood; 
	private Boolean btxt_signup_nama;
 	private Boolean btxt_signup_email;
 	private Boolean btxt_signup_password;
 	private Boolean btxt_signup_password2;
 	private Boolean btxt_signup_country;
 	private Boolean btxt_signup_phone;
 	private Boolean btxt_signup_city; 

	private EditText txt_countri;
	
	private ArrayList<Countries_Array> countrieslist;
	private ArrayList<Countries_Array> countrieslist2;
	final List<String> list = new ArrayList<String>();
	final List<String> list2 = new ArrayList<String>();
	
	public static double lat;
	public static double lng;
	
	Drawable errorIcon;
	

	AlertDialogManager alert = new AlertDialogManager();
 
	/*
	@Override
	public void onBackPressed() { 
		//overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
		
		Toast.makeText(getApplicationContext(), "back",
				3000).show();
		Register.this.finish();
	}
	*/
	
	
	@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
		Toast.makeText(getApplicationContext(), "back",
				3000).show();
		//Register.this.finish();
    }
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
        setContentView(R.layout.signup);
         

		
        inisialisasi();	
        form_error();
        form_click();
        session();

		/*
        spinnerFood = (Spinner) findViewById(R.id.spinFood);
        //spinnerFood.setOnItemSelectedListener(this);
        countrieslist = new ArrayList<Countries_Array>();

        
        //new GetCategories().execute();
       
 
        spinnerFood.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				// TODO Auto-generated method stub
				//Toast.makeText(getBaseContext(), list.get(arg2).toString(),Toast.LENGTH_SHORT).show();
				 
				
				//System.out.println("print array list");
				String selec = spinnerFood.getSelectedItem().toString();
				String phonecode ="";
				Boolean exist = false;
				for(int i = 0; i < countrieslist.size(); i++) 
				{
					
					String select_array = countrieslist.get(i).getcountryName().toString();
					if(select_array.equalsIgnoreCase(selec)){
						////System.out.println((countrieslist.get(i).getcountryName()).toString());
						////System.out.println((countrieslist.get(i).getcountryPhone()).toString());
						
						phonecode = countrieslist.get(i).getcountryPhone().toString();
						exist=true;
			            break;
        			}
					
				} 
				
				if(exist) {
					txt_signup_city.setText(phonecode);
				} else {
				     
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		}); 
        */ 
        
        
    }
	
	
	private void inisialisasi(){ 
		lbl_signup_judul = (TextView) findViewById(R.id.lbl_signup_judul);
		lbl_signup_country = (TextView) findViewById(R.id.lbl_signup_country);
        txt_signup_nama = (EditText) findViewById(R.id.txt_signup_nama);
        txt_signup_email = (EditText) findViewById(R.id.txt_signup_email);
        txt_signup_password = (EditText) findViewById(R.id.txt_signup_password);
        txt_signup_password2 = (EditText) findViewById(R.id.txt_signup_password2);
        txt_signup_country = (EditText) findViewById(R.id.txt_signup_country);
        txt_signup_phone = (EditText) findViewById(R.id.txt_signup_phone);
        txt_signup_city = (EditText) findViewById(R.id.txt_signup_city);
        cmd_signup_submit = (Button) findViewById(R.id.cmd_signup_submit); 
        
        ImageView diskonion_iconmenu = (ImageView) findViewById(R.id.diskonion_iconmenu); 
		
        Typeface font = Typeface.createFromAsset(getAssets(), "gisha.ttf");  
         
		lbl_signup_judul.setTypeface(font);
		lbl_signup_country.setTypeface(font);
        txt_signup_nama.setTypeface(font);
        txt_signup_email.setTypeface(font);
        txt_signup_password.setTypeface(font);
        txt_signup_password2.setTypeface(font);
        txt_signup_country.setTypeface(font);
        txt_signup_phone.setTypeface(font);
        txt_signup_city.setTypeface(font);
        cmd_signup_submit.setTypeface(font); 
        
		diskonion_iconmenu.setOnClickListener(new OnClickListener() { 
	 			public void onClick(View v)  {
	 				Intent intent=new Intent(getApplicationContext(),Signin.class);
	 				startActivity(intent); 
                    finish();
			}
		});
         
        
        cmd_signup_submit.setEnabled(false);
        cmd_signup_submit.setClickable(false); 
        cmd_signup_submit.setBackgroundDrawable(getResources().getDrawable(R.drawable.custom_btn_seagull_round2));

		errorIcon = getResources().getDrawable(R.drawable.image_error);
		errorIcon.setBounds(new Rect(0, 0, errorIcon.getIntrinsicWidth(), errorIcon.getIntrinsicHeight()));
		

		//txt_nama.setError(null,errorIcon);
		txt_signup_nama.setError("Empty value");
		txt_signup_email.setError("Empty value");
		txt_signup_password.setError("Empty value");
		txt_signup_password2.setError("Empty value");
		txt_signup_country.setError("Empty value");
		txt_signup_phone.setError("Empty value");
        txt_signup_city.setError("Empty value");
		
        btxt_signup_nama = false;
		btxt_signup_email = false;
		btxt_signup_password = false;
		btxt_signup_password2 = false;
		btxt_signup_country = false;
		btxt_signup_phone = false;
		btxt_signup_city = false;
		 
	}
	
	private void session(){
		
		//ngurusin session
		session = new SessionManager(getApplicationContext());
	    
	    HashMap<String, String> user = session.getUserDetails();
	     
	    // name
	    session_userid = user.get(SessionManager.id_users);
	    session_username = user.get(SessionManager.username);
	    session_password = user.get(SessionManager.password);
	    session_email = user.get(SessionManager.email);
	    session_city = user.get(SessionManager.city);
	    session_checked = user.get(SessionManager.checked);
	    session_channel = user.get(SessionManager.channel);
	    session_category = user.get(SessionManager.category);
		
	    // getting intent data
        Intent in = getIntent();
         
        // Get XML values from previous intent
        id_ = in.getStringExtra(KEY_CHANNEL_ID);
        //String title_ = in.getStringExtra(KEY_MERCHANT_NAME);
        //String artist_ = in.getStringExtra(KEY_VOUCHER_NAME);
        //String duration_ = in.getStringExtra(KEY_VOUCHER_TOTAL);
        //String thumb_url_ = in.getStringExtra(KEY_MERCHANT_PHOTO);
        // new loadListView().execute();
	}
	 
	private void form_error(){
		 
		txt_signup_email.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub
				 
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				if (txt_signup_email.getText().toString() == null) {
					txt_signup_email.requestFocus();
					txt_signup_email.setError("Insert right email!");

					btxt_signup_email = false; 
			        cmd_signup_submit.setEnabled(false);
					cmd_signup_submit.setClickable(false); 
					cmd_signup_submit.setBackgroundDrawable(getResources().getDrawable(R.drawable.custom_btn_seagull_gray));
					
				} else if (isEmailValid(txt_signup_email.getText().toString()) == false) {
					txt_signup_email.requestFocus();
					txt_signup_email.setError("Insert right email!");
					
					btxt_signup_email = false; 
			        cmd_signup_submit.setEnabled(false);
					cmd_signup_submit.setClickable(false); 
					cmd_signup_submit.setBackgroundDrawable(getResources().getDrawable(R.drawable.custom_btn_seagull_gray));
					 
				} else {
					//System.out.println("boolean true 1");

					//System.out.println("txt_signup_email "+txt_signup_email.getError()+" |");
					//System.out.println("txt_signup_password "+txt_signup_password.getError()+" |");
					//System.out.println("txt_signup_password2 "+txt_signup_password2.getError()+" |");
					//System.out.println("txt_signup_phone "+txt_signup_phone.getError()+" |");
					//System.out.println("txt_signup_city "+txt_signup_city.getError()+" |"); 
					if((txt_signup_email.getError() == null)&&(txt_signup_password.getError() == null)&&(txt_signup_password2.getError() == null)&&(txt_signup_phone.getError() == null)&&(txt_signup_city.getError() == null))
					{
						//System.out.println("masuk boolean true 1"); 
				        //cmd_signup_submit.setEnabled(true);
						//cmd_signup_submit.setClickable(true); 
						//cmd_signup_submit.setBackgroundDrawable(getResources().getDrawable(R.drawable.custom_btn_seagull));
					} 
					/**
					if(txt_signup_email.getError() == "")
					{ 
						btxt_signup_email = true;
					} 
					//System.out.println(btxt_signup_email);
					//System.out.println(btxt_signup_password);
					//System.out.println(btxt_signup_password2);
					//System.out.println(btxt_signup_phone);
					//System.out.println(btxt_signup_city);
					if((btxt_signup_email=true)&&(btxt_signup_password=true)&&(btxt_signup_password2=true)&&(btxt_signup_phone=true)&&(btxt_signup_city=true))
					{
						txt_check.setEnabled(true);
						txt_check.setClickable(true); 
				        cmd_signup_submit.setEnabled(true);
						cmd_signup_submit.setClickable(true); 
						cmd_signup_submit.setBackgroundDrawable(getResources().getDrawable(R.drawable.custom_btn_seagull));
					} 
					*/
				}
			}
		});
		
		txt_signup_password.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				if(txt_signup_password.getText().length()<6)
		    	{
					txt_signup_password.setError("Min 6 digit!");

					btxt_signup_password = false; 
			        cmd_signup_submit.setEnabled(false);
					cmd_signup_submit.setClickable(false); 
					cmd_signup_submit.setBackgroundDrawable(getResources().getDrawable(R.drawable.custom_btn_seagull_gray));
		    	}
				else
				{
					//System.out.println(" boolean true 2");
					//System.out.println("txt_signup_email "+txt_signup_email.getError()+" |");
					//System.out.println("txt_signup_password "+txt_signup_password.getError()+" |");
					//System.out.println("txt_signup_password2 "+txt_signup_password2.getError()+" |");
					//System.out.println("txt_signup_phone "+txt_signup_phone.getError()+" |");
					//System.out.println("txt_signup_city "+txt_signup_city.getError()+" |"); 
					
					if((txt_signup_email.getError() == null)&&(txt_signup_password.getError() == null)&&(txt_signup_password2.getError() == null)&&(txt_signup_phone.getError() == null)&&(txt_signup_city.getError() == null))
					{
						//System.out.println("masuk boolean true 2"); 
				        //cmd_signup_submit.setEnabled(true);
						//cmd_signup_submit.setClickable(true); 
						//cmd_signup_submit.setBackgroundDrawable(getResources().getDrawable(R.drawable.custom_btn_seagull));
					} 
					/*
					if(txt_signup_password.getError() == "")
					{ 
						btxt_signup_password = true;
					}
					//System.out.println(btxt_signup_email);
					//System.out.println(btxt_signup_password);
					//System.out.println(btxt_signup_password2);
					//System.out.println(btxt_signup_phone);
					//System.out.println(btxt_signup_city);
					if((btxt_signup_email=true)&&(btxt_signup_password=true)&&(btxt_signup_password2=true)&&(btxt_signup_phone=true)&&(btxt_signup_city=true))
					{
						txt_check.setEnabled(true);
						txt_check.setClickable(true);
				        cmd_signup_submit.setEnabled(true);
						cmd_signup_submit.setClickable(true); 
						cmd_signup_submit.setBackgroundDrawable(getResources().getDrawable(R.drawable.custom_btn_seagull));
					} 
					*/
				}
			}
		});
		
		txt_signup_password2.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				if(txt_signup_password2.getText().length()<6)
		    	{
					txt_signup_password2.setError("Min 6 digit!");
					
					btxt_signup_password2 = false; 
			        cmd_signup_submit.setEnabled(false);
					cmd_signup_submit.setClickable(false); 
					cmd_signup_submit.setBackgroundDrawable(getResources().getDrawable(R.drawable.custom_btn_seagull_gray));
		    	}
				else if(!txt_signup_password2.getText().toString().equals(txt_signup_password.getText().toString()))
				{
					//System.out.println("1."+txt_signup_password.getText());
					//System.out.println("2."+txt_signup_password2.getText());
					txt_signup_password2.setError("Password tidak sama!");
					
					btxt_signup_password2 = false; 
			        cmd_signup_submit.setEnabled(false);
					cmd_signup_submit.setClickable(false); 
					cmd_signup_submit.setBackgroundDrawable(getResources().getDrawable(R.drawable.custom_btn_seagull_gray));
				}
				else
				{
					//System.out.println(" boolean true 3");
					//System.out.println("txt_signup_email "+txt_signup_email.getError()+" |");
					//System.out.println("txt_signup_password "+txt_signup_password.getError()+" |");
					//System.out.println("txt_signup_password2 "+txt_signup_password2.getError()+" |");
					//System.out.println("txt_signup_phone "+txt_signup_phone.getError()+" |");
					//System.out.println("txt_signup_city "+txt_signup_city.getError()+" |"); 
					if((txt_signup_email.getError() == null)&&(txt_signup_password.getError() == null)&&(txt_signup_password2.getError() == null)&&(txt_signup_phone.getError() == null)&&(txt_signup_city.getError() == null))
					{
						//System.out.println("masuk boolean true 3"); 
				        //cmd_signup_submit.setEnabled(true);
						//cmd_signup_submit.setClickable(true); 
						//cmd_signup_submit.setBackgroundDrawable(getResources().getDrawable(R.drawable.custom_btn_seagull));
					} 
					/*
					if(txt_signup_password2.getError() == "")
					{ 
						btxt_signup_password2 = true;
					}
					//System.out.println(btxt_signup_email);
					//System.out.println(btxt_signup_password);
					//System.out.println(btxt_signup_password2);
					//System.out.println(btxt_signup_phone);
					//System.out.println(btxt_signup_city);
					if((btxt_signup_email=true)&&(btxt_signup_password=true)&&(btxt_signup_password2=true)&&(btxt_signup_phone=true)&&(btxt_signup_city=true))
					{
						txt_check.setEnabled(true);
						txt_check.setClickable(true);
				        cmd_signup_submit.setEnabled(true);
						cmd_signup_submit.setClickable(true); 
						cmd_signup_submit.setBackgroundDrawable(getResources().getDrawable(R.drawable.custom_btn_seagull));
					} 
					*/
				}
			}
		});
		
		txt_signup_phone.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				int MinLen=6;
				int MaxLen=20;
				if (txt_signup_phone.getText().toString().length() <= 0) {
					txt_signup_phone.setError("Insert only numeric!"); 
					
					btxt_signup_phone = false; 
			        cmd_signup_submit.setEnabled(false);
					cmd_signup_submit.setClickable(false); 
					cmd_signup_submit.setBackgroundDrawable(getResources().getDrawable(R.drawable.custom_btn_seagull_gray));
				} 
				else if (Double.valueOf(txt_signup_phone.getText().toString()) < MinLen || Double.valueOf(txt_signup_phone.getText().length()) > MaxLen) {
					txt_signup_phone.setError("Long of character wrong!"); 

					btxt_signup_phone = false; 
			        cmd_signup_submit.setEnabled(false);
					cmd_signup_submit.setClickable(false); 
					cmd_signup_submit.setBackgroundDrawable(getResources().getDrawable(R.drawable.custom_btn_seagull_gray));
				} 
				else
				{
					//System.out.println(" boolean true 4");
					//System.out.println("txt_signup_email "+txt_signup_email.getError()+" |");
					//System.out.println("txt_signup_password "+txt_signup_password.getError()+" |");
					//System.out.println("txt_signup_password2 "+txt_signup_password2.getError()+" |");
					//System.out.println("txt_signup_phone "+txt_signup_phone.getError()+" |");
					//System.out.println("txt_signup_city "+txt_signup_city.getError()+" |"); 
					
					if((txt_signup_email.getError() == null)&&(txt_signup_password.getError() == null)&&(txt_signup_password2.getError() == null)&&(txt_signup_phone.getError() == null)&&(txt_signup_city.getError() == null))
					{
						//System.out.println("masuk boolean true 4"); 
				        //cmd_signup_submit.setEnabled(true);
						//cmd_signup_submit.setClickable(true); 
						//cmd_signup_submit.setBackgroundDrawable(getResources().getDrawable(R.drawable.custom_btn_seagull));
					} 
					/*if(txt_signup_phone.getError() == "")
					{ 
						btxt_signup_phone = true;
					}
					//System.out.println(btxt_signup_email);
					//System.out.println(btxt_signup_password);
					//System.out.println(btxt_signup_password2);
					//System.out.println(btxt_signup_phone);
					//System.out.println(btxt_signup_city);
					if((btxt_signup_email=true)&&(btxt_signup_password=true)&&(btxt_signup_password2=true)&&(btxt_signup_phone=true)&&(btxt_signup_city=true))
					{
						txt_check.setEnabled(true);
						txt_check.setClickable(true);
				        cmd_signup_submit.setEnabled(true);
						cmd_signup_submit.setClickable(true); 
						cmd_signup_submit.setBackgroundDrawable(getResources().getDrawable(R.drawable.custom_btn_seagull));
					} 
					*/
				}
			}
		});
		
		
		txt_signup_city.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}
			 

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				int MinLen=6;
				int MaxLen=20;
				////System.out.println("di dalam spiner");
				////System.out.println(txt_signup_city.getText().toString());
				////System.out.println(txt_signup_city.getText().toString().length() );
				if (txt_signup_city.getText().toString().length() <= 0) {
					txt_signup_city.setError("Insert only numeric!"); 
					
					btxt_signup_city = false; 
			        cmd_signup_submit.setEnabled(false);
					cmd_signup_submit.setClickable(false); 
					cmd_signup_submit.setBackgroundDrawable(getResources().getDrawable(R.drawable.custom_btn_seagull_gray));
				}  
				else
				{
					//System.out.println(" boolean true 5"); 
					txt_signup_city.setError(null);

					//System.out.println("txt_signup_email "+txt_signup_email.getError()+" |");
					//System.out.println("txt_signup_password "+txt_signup_password.getError()+" |");
					//System.out.println("txt_signup_password2 "+txt_signup_password2.getError()+" |");
					//System.out.println("txt_signup_phone "+txt_signup_phone.getError()+" |");
					//System.out.println("txt_signup_city "+txt_signup_city.getError()+" |"); 
					if((txt_signup_email.getError() == null)&&(txt_signup_password.getError() == null)&&(txt_signup_password2.getError() == null)&&(txt_signup_phone.getError() == null)&&(txt_signup_city.getError() == null))
					{
						//System.out.println("masuk boolean true 5");  
				        //cmd_signup_submit.setEnabled(true);
						//cmd_signup_submit.setClickable(true); 
						//cmd_signup_submit.setBackgroundDrawable(getResources().getDrawable(R.drawable.custom_btn_seagull));
					} 
					
					/*
					if(txt_signup_city.getError() == "")
					{
						btxt_signup_city = true;
						//System.out.println("btxt_signup_city "+btxt_signup_city);
					}
					*/

					/*
					//System.out.println(btxt_signup_email);
					//System.out.println(btxt_signup_password);
					//System.out.println(btxt_signup_password2);
					//System.out.println(btxt_signup_phone);
					//System.out.println(btxt_signup_city);
					if((btxt_signup_email=true)&&(btxt_signup_password=true)&&(btxt_signup_password2=true)&&(btxt_signup_phone=true)&&(btxt_signup_city=true))
					{
						txt_check.setEnabled(true);
						txt_check.setClickable(true);
				        cmd_signup_submit.setEnabled(true);
						cmd_signup_submit.setClickable(true); 
						cmd_signup_submit.setBackgroundDrawable(getResources().getDrawable(R.drawable.custom_btn_seagull));
					} 
					*/
				}
			}
		});
		 
	}
	
	public void form_click(){
		 
		 

		cmd_signup_submit.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) 
				{
					    
					//System.out.println("click");
					new Send().execute();
				}
		});
		
		 
	}
	
	 
	 
	  
	@SuppressLint("NewApi")
	class Send extends AsyncTask {

		private String json = null;
		private JSONObject jsonobject;
		private String code;
		private String msg;
		public String url2;

		@Override
		protected void onPreExecute() {
			super.onPreExecute(); 
			pDialog = new ProgressDialog(Signup.this);
			pDialog.setMessage("Please wait...");
			pDialog.setCancelable(false);
			pDialog.show();
			
			url2 = Global.link_register + 
					"user="+txt_signup_email.getText().toString()+"&"+ 
					"pass="+txt_signup_password.getText().toString()+"&"+ 
					"country="+spinnerFood.getSelectedItem().toString()+"&"+ 
					"area="+txt_signup_city.getText().toString()+"&"+ 
					"hp="+txt_signup_phone.getText().toString();
			
			 
			url2 = url2.replace(" ", "%20"); 
			//System.out.println(url2);

		}

		@Override
		protected Object doInBackground(Object... params) {
			code="99";
			msg="Not connected to server";

			System.out.println(url2);
			try{  
				json=JSONfunctions.getStringContent2(url2);
				System.out.println(json);
				jsonobject=new JSONObject(json);
				
				code=jsonobject.getString("code");
				msg=jsonobject.getString("msg");
				 
			}
			catch (Exception e) 
			{
				e.printStackTrace();
				code="99";
				msg="Not connected to server";
				
				//System.out.println("protected Object doInBackground - catch");
				//System.out.println("code "+code);
				//System.out.println("msg "+msg);
			}
			return null;
		}

		@Override
		protected void onPostExecute(Object result) {
			super.onPostExecute(result);
			if (code.equalsIgnoreCase("1")) {
				//alert.showAlertDialog(Register.this, "Notification", "Registration success, check your email for activation.", true);
				
				AlertDialog.Builder alertDialog = new AlertDialog.Builder(Signup.this); 
	            alertDialog.setTitle("Notification"); 
	            alertDialog.setMessage("Registration success, check your email for activation."); 
	            alertDialog.setIcon(R.drawable.image_success); 
	            alertDialog.setPositiveButton("OK",
	                    new DialogInterface.OnClickListener() {
	                        public void onClick(DialogInterface dialog,int which) {
	                            // Write your code here to execute after dialog
	                        	Intent i = new Intent(getApplicationContext(), Signin.class);
	                            startActivity(i); 
	                            finish();
	                        }
	                    });
	            

	            // Showing Alert Message
	            alertDialog.show();
				   
			} else {
				//Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
				//alert.showAlertDialog(Register.this, "Notification", msg, false);
				
				AlertDialog.Builder alertDialog = new AlertDialog.Builder(Signup.this); 
	            alertDialog.setTitle("Notification"); 
	            alertDialog.setMessage(msg); 
	            alertDialog.setIcon(R.drawable.image_fail); 
	            alertDialog.setPositiveButton("OK",
	                    new DialogInterface.OnClickListener() {
	                        public void onClick(DialogInterface dialog,int which) {
	                            // Write your code here to execute after dialog
	                        	Intent i = new Intent(getApplicationContext(), Signin.class);
	                            startActivity(i); 
	                            finish();
	                        }
	                    });
	            

	            // Showing Alert Message
	            alertDialog.show();
			}
			

			if (pDialog.isShowing())
				pDialog.dismiss();
			 
			
		}

	}
	
	boolean isEmailValid(CharSequence email) {
		return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
	} // end of email matcher


	
	private String getMyPhoneNumber(){
	    TelephonyManager mTelephonyMgr;
	    mTelephonyMgr = (TelephonyManager)
	        getSystemService(Context.TELEPHONY_SERVICE); 
	    return mTelephonyMgr.getLine1Number();
	}

	private String getMy10DigitPhoneNumber(){
	    String s = getMyPhoneNumber();
	    return s.substring(2);
	}
	
	public boolean isonline()
	{
		ConnectivityManager cm=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo ni=cm.getActiveNetworkInfo();
		if(ni != null && ni.isConnected())
		{
			return true;
		}
		return false;
		
	}
	
	 
	     

	   
		private void populateSpinner() {
			List<String> lables = new ArrayList<String>();
			List<String> lables2 = new ArrayList<String>();
			
			 

			for (int i = 0; i < countrieslist.size(); i++) {
				lables.add(countrieslist.get(i).getcountryName());
			}

			 
			
			// Creating adapter for spinner
			ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
					android.R.layout.simple_spinner_item, lables);
			
			 
			// Drop down layout style - list view with radio button
			spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			 
			// attaching data adapter to spinner
			spinnerFood.setAdapter(spinnerAdapter); 
		}

		 
		/**
		 * Async task to get all food categories
		 * */
		private class GetCategories extends AsyncTask<Void, Void, Void> {

		 

			protected void onPreExecute() {
				
				super.onPreExecute();
				pDialog = new ProgressDialog(Signup.this);
				pDialog.setMessage("Getting data...");
				pDialog.setCancelable(false);
				pDialog.show();
				 
			}
			
			
			protected Void doInBackground(Void... params) {
				
				Global.URL = Global.link_countries;
				//System.out.println(Global.URL);  
				Global.URL = Global.URL.replace(" ", "%20");

				code="99";
				msg="Not connected to server";
				//System.out.println("Mulai do in background");
	 
				try{ 
					json=JSONfunctions.getStringContent(Global.URL);
					jsonobject=new JSONObject(json); 
					code=jsonobject.getString("code");
					msg=jsonobject.getString("msg");
					 
				}
				catch (Exception e) 
				{
					e.printStackTrace();
					System.out.println("catch code msg");
					System.out.println(e);
					//pDialog.dismiss();
					//Toast.makeText(getApplicationContext(), "Something wrong in your connection", Toast.LENGTH_LONG).show();
				
				}
				
				System.out.println("code "+code);
				if(code.equalsIgnoreCase("99")){
					try{ 
						System.out.println(Global.URL);
						//json=JSONfunctions.getStringContent2(Global.URL);
						System.out.println(json); 
						 
					}
					catch (Exception e) 
					{
						e.printStackTrace();
						System.out.println("catch 1");
						System.out.println(e);

						//pDialog.dismiss();
						//Toast.makeText(getApplicationContext(), "Something wrong in your connection (1)", Toast.LENGTH_LONG).show();
					
					}
				}
				
				return null;   
			}
			
			@Override
			protected void onPostExecute(Void unused) {
				
				//System.out.println("onpostexecute"); 
				count = 0;  
				result = json;
				 

				if(code.equalsIgnoreCase("0")){
					
					Toast.makeText(getBaseContext(), msg, Toast.LENGTH_LONG).show();
				}
				else{
					//System.out.println("masuk else null"); 
					try{
						
				      	JSONArray jArray = new JSONArray(result);
				      	JSONObject json_data=null; 
				      	//System.out.println("array length");
				      	//System.out.println(jArray.length());
				      	
				      	for(int i=0;i<jArray.length();i++){
							json_data = jArray.getJSONObject(i);  
							xml_countries_id = json_data.getString(KEY_COUNTRIES_ID);  
							xml_countries_code = json_data.getString(KEY_COUNTRIES_CODE);  
							xml_countries_name = json_data.getString(KEY_COUNTRIES_NAME);  
							xml_countries_phone = json_data.getString(KEY_COUNTRIES_PHONE);  
							////System.out.println(xml_countries_id);  
							////System.out.println(xml_countries_code);  
							////System.out.println(xml_countries_name);  
							////System.out.println(xml_countries_phone);  
							//adapter.add(new SampleItem2(xml_countries_id, xml_countries_code, xml_countries_name));
							
							Countries_Array cat = new Countries_Array(xml_countries_id, xml_countries_code, xml_countries_name, xml_countries_phone);
							countrieslist.add(cat); 
							
							list.add(xml_countries_name); 
							Collections.sort(list); 
				      	} 
				      	
					}catch(JSONException e1){ 
						//System.out.println(e1);
						//System.out.println("catch 3");
						Toast.makeText(getApplicationContext(), "No Data Found", Toast.LENGTH_LONG).show();
					
					}catch (ParseException e1){
						//System.out.println("catch 4");
						e1.printStackTrace();
					} 
				}
				 
				
				if (pDialog.isShowing())
					pDialog.dismiss();
				populateSpinner();
				 
			}


			 
		}
		
		
		 
 
	 
}
